<template>
  <router-view v-slot="{ Component }">
    <transition name="move" mode="out-in">
      <keep-alive exclude="">
        <div>
          <!-- <el-config-provider :locale="locale"> -->
          <component :is="Component" v-if="isRouterActive" :key="$route.name" />
          <!-- </el-config-provider> -->
        </div>
      </keep-alive>
    </transition>
  </router-view>
</template>

<script>
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElConfigProvider } from "element-plus";
import { GM_SM4Decrypt } from "./utils/sm4Import";
export default {
  name: "App",
  provide() {
    return {
      globalReload: this.globalReload,
    };
  },
  data() {
    return {
      isRouterActive: true,
      locale: zhCn,
      whole_timeout: null,
      lastTime: null,
      currentTime: "",
      // 不操作页面多长时间后退出 默认1个小时
      sys_time: 60 * 60 * 1000,
      // 执行定时器的时间间隔  两分钟检查一次页面是否退出
      check_time: 2 * 60 * 1000
    };
  },
  components: {
    ElConfigProvider,
  },
  methods: {
    globalReload() {
      this.isRouterActive = false;
      this.$nextTick(function () {
        this.isRouterActive = true;
      });
    },
    isTimeout() {
      // getTime()方法获取的是毫秒数
      // 配置文件中超时间隔
      let configParamStrDec = JSON.parse(GM_SM4Decrypt(paramsStr))
      // console.log('configParamStrDec', configParamStrDec.timeoutExit)
      this.sys_time = configParamStrDec.timeoutExit * 1000 *60
      this.lastTime = this.$store.state.lastTime
      this.currentTime = (new Date()).getTime()
      // console.log(this.lastTime, this.currentTime)
      let intervalTime = this.currentTime - this.lastTime
      // console.log(intervalTime, this.sys_time)
      if (intervalTime >= this.sys_time)
        return true
      else
        return false
    }
  },
  created() {
  },
  mounted() {
    console.log("App加载")
    this.$router.afterEach((to, from) => {
      // console.log(to, from)
      if (to.path != '/login' && to.path != '/') {
        // 先清除一下定时器
        clearInterval(this.whole_timeout)
        // 定时执行超时退出
        this.whole_timeout = setInterval(() => {
          // console.log(this.isTimeout())
          if (this.isTimeout()) {
            // 超时退出
            console.log("超时自动退出！")
            this.$store.dispatch("app/logout");
            // 退出再清除一下定时器
            clearInterval(this.whole_timeout)
          } else {
            // 没到时间间隔不做处理
            console.log("没有超时不做处理！")
          }
        }, this.check_time)
      } else {
        // 关闭定时器
        clearInterval(this.whole_timeout)
      }
    })
  },
  destroyed() {
    clearInterval(this.whole_timeout)
  },
};
</script>
<style lang="scss" scoped></style>
